package com.example.my;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private WebView my_webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        my_webview = findViewById(R.id.my_webview);
//        my_webview.loadUrl("http://www.baidu.com");
//        my_webview.loadUrl("http://ica.gdcp.cn/web/loadWebPage.do?1_649_44015038150_3");
//        my_webview.loadUrl("file:///android_asset/test.html");

        /*String htmlContent="<html>\n" +
                "<head>\n" +
                "<title></title>\n" +
                "</head>\n" +
                "<body>\n" +
                "hello world!\n" +
                "<img src=\"file:///android_asset/dog3.jpg\" width=\"100\" height=\"100\" />\n" +
                "</body>\n" +
                "</html>";
        my_webview.loadDataWithBaseURL(null,htmlContent,"text/html","utf-8",null);
        */

        my_webview.loadUrl("file:///mnt/sdcard/test.html");

        my_webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("MainActiviy","url="+url);
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.e("MainActiviy","开始加载页面");
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.e("MainActivity","页面加载完成");
                super.onPageFinished(view, url);
            }
        });
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        if (my_webview.canGoBack()){
            my_webview.goBack();
        }else {
            super.onBackPressed();
        }
    }

    /*@Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e("MainActiviy","收到Touch事件"+event.getAction()+"坐标"+event.getX()+"  "+event.getY());
        return super .onTouchEvent(event);
    }
    */
}
